/**
 * Created by 10520_000 on 11/5/14.
 */
//class error ui-inputPassword error

//<div class="box s-error mbs msgBox l-pageWrapper mbm">
//    <div class="pam">Đăng kí tài khoản thất bại</div>
//</div>


$(document).ready(function(){
//   event
   bindEventRegister();
});

function bindEventRegister() {
   ISS.EVENT.bindEvent('#send_Info_User', 'click', function () {
      clearValidate();
      if(validate()){
         insertInfo();
      }
      else {
         var htmlText = '<div id = "message-error" class="box s-error mbs msgBox l-pageWrapper mbm">\
                                <div class="pam">Đăng kí tài khoản thất bại</div>\
                            </div>';
         ISS.DOM.before('.l-register__benefits', htmlText);
      }
   });

   ISS.EVENT.bindEvent('#RegistrationForm_gender_1', 'click', function () {
      ISS.DOM.removeAttr('#RegistrationForm_gender_0', 'checked');
      ISS.DOM.setAtt('#RegistrationForm_gender_1', 'checked','checked');
   });

   ISS.EVENT.bindEvent('#RegistrationForm_gender_0', 'click', function () {
      ISS.DOM.removeAttr('#RegistrationForm_gender_1', 'checked');
      ISS.DOM.setAtt('#RegistrationForm_gender_0', 'checked','checked');
   });
}

function validate() {
   var email, pass, passRetype, result;
    email = ISS.DOM.getVal('#RegistrationForm_email');
    pass = ISS.DOM.getVal('#RegistrationForm_password');
    passRetype = ISS.DOM.getVal('#RegistrationForm_password2');

   if(!email) {
       ISS.DOM.addClass('#RegistrationForm_email', 'error');
      result = false;
   }

    if(!pass) {
        ISS.DOM.addClass('#RegistrationForm_password', 'error');
       result = false;
    }

    if(!passRetype) {
        ISS.DOM.addClass('#RegistrationForm_password2', 'error');
       result = false;
    }

   if(email && pass && passRetype) {
      if(passRetype != pass) {
         ISS.DOM.addClass('#RegistrationForm_password2', 'error');
         result = false;
      } else {
         if(!ISS.VALIDATION.validateEmail(email, true)){
            ISS.DOM.addClass('#RegistrationForm_email', 'error');
            result = false;
         }
         else {
            result = true;
         }
      }
   }
   return result;
}

function insertInfo() {
   var phanQuyen = {}, _thongTinThanhVien = {}, day, month, year;
    day = ISS.DOM.getVal('#RegistrationForm_day');
    month = ISS.DOM.getVal('#RegistrationForm_month');
    year = ISS.DOM.getVal('#RegistrationForm_year');
    var dateString = year + '/' + month + '/' + day;
   _thongTinThanhVien.ten = ISS.DOM.getVal('#RegistrationForm_first_name');
   _thongTinThanhVien.ho = ISS.DOM.getVal('#RegistrationForm_last_name');
   _thongTinThanhVien.gioiTinh = ISS.DOM.getVal('input:radio[name=RegistrationForm[gender]]:checked');
   _thongTinThanhVien.maTV = ISS.DOM.getVal('#RegistrationForm_email');
   _thongTinThanhVien.ngaySinh = new Date(dateString);
   phanQuyen.thongTinThanhVien = _thongTinThanhVien;
   phanQuyen.matKhau = ISS.DOM.getVal('#RegistrationForm_password');

   ISS.BACKEND.request({
      url: '../../service/header/register',
      headers: {
         'Accept': 'application/json',
         'Content-Type': 'application/json'
      },
      type: 'POST',
      dataType: 'json',
      data: JSON.stringify(phanQuyen),
      success: function (data) {
         var responseContent = data.responseObject;
         insertInfoComplete(responseContent);
      }
   });
}

function insertInfoComplete(responseInfo) {
   if(responseInfo) {
      ISS.WINDOW.location('/jsp/user/user-account.jsp');
   }
   else {
      var htmlText = '<div id="message-error" class="box s-error mbs msgBox l-pageWrapper mbm">\
                                <div class="pam">Email hợp lệ</div>\
                            </div>';
      ISS.DOM.before('.l-register__benefits', htmlText);
   }
}

function clearValidate() {
   ISS.DOM.remove('#message-error');
    ISS.DOM.removeClass('#RegistrationForm_email', 'error');
    ISS.DOM.removeClass('#RegistrationForm_password', 'error');
    ISS.DOM.removeClass('#RegistrationForm_password2', 'error');
}
